package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.Warehouseinfo;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ParameterException;
import com.woniuxy.model.PawnWarehouse;
import com.woniuxy.service.PawnWarehouseService;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@RestController
@Api(tags = "仓库相关接口")
@RequestMapping("/pawnWarehouse")
public class PawnWarehouseController {
    @Resource
    private PawnWarehouseService warehouseService;

    /**
     * 得到所有仓库对象，
     * @return
     */
    @ApiOperation("查找所有仓库")
    @GetMapping("/getAllwh")
    public Result findAllWarehouse(){

        return Result.success(200,"查询成功",warehouseService.list());
    }


    /**
     * 分页查询所有仓库
     */
    @ApiOperation("分页查找所有仓库")
    @GetMapping("/getAllwhpage")
    public Result findAllWhpage(FindAllWhpageform findAllWhpageform){

        return warehouseService.findAllPage(findAllWhpageform);
    }

    /**
     * 分页模糊查询所有仓库
     */
    @ApiOperation("分页模糊查找所有仓库")
    @GetMapping("/getWhByLike")
    public Result getWhByLike(FindWhByLikefrom findShopByLikefrom){

        return warehouseService.findWhpageBylike(findShopByLikefrom);
    }

    /**
     * 删除传来会的仓库对象对应的仓库
     * @param deleWhListfrom
     * @return
     * @throws DdException
     */
    @ApiOperation("删除仓库")
    @PostMapping("/delWhById")
    public Result deleWhList(@RequestBody DeleWhListfrom deleWhListfrom) throws DdException {

    return warehouseService.deleWhList(deleWhListfrom);
    }

    /**
     * 更新仓库
     * @param upateWhform
     * @return
     */
    @ApiOperation("更新仓库信息")
    @PostMapping("/updateWh")
    public Result updateShop(@RequestBody UpateWhform upateWhform ){
        Warehouseinfo warehouseform = upateWhform.getWarehouseform();
        System.out.println(warehouseform);
        PawnWarehouse pawnWarehouse = new PawnWarehouse();
        pawnWarehouse.setWhCode(warehouseform.getWhCode());
        pawnWarehouse.setAddress(warehouseform.getAddress());
        pawnWarehouse.setContact(warehouseform.getContact());
        pawnWarehouse.setPhoneNo(warehouseform.getPhoneNo());
        pawnWarehouse.setWhName(warehouseform.getWhName());
        pawnWarehouse.setWhStat(warehouseform.getWhStat());
//        PawnWarehouse warehouse = NewCopyBean.copyOne(warehouseform, PawnWarehouse.class);
//        System.out.println(warehouse);
        warehouseService.update(pawnWarehouse,new QueryWrapper<PawnWarehouse>().eq("wh_code",warehouseform.getWhCode()));
        return Result.success(200,"修改成功",null);
    }

    /**
     * 新增仓库
     * @param addWhfrom
     * @param bindingResult
     * @return
     * @throws DdException
     */
    @ApiOperation("新增仓库仓库")
    @PostMapping("/addwh")
    public Result addShop(@RequestBody @Valid AddWhfrom addWhfrom,
                          BindingResult bindingResult) throws DdException {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            String s="";
            for (ObjectError allError : allErrors) {
                s = allError.getDefaultMessage();
            }
            throw new ParameterException(500003, s);
        }
        return warehouseService.addWh(addWhfrom);
    }
}

